package com.zzyl.nursing.service.impl;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;

import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.CheckIn;

import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.mapper.CheckInMapper;
import com.zzyl.nursing.vo.NursingTaskVo;
import com.zzyl.system.mapper.SysUserMapper;
import com.zzyl.utils.IDCardUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 护理任务Service业务层处理
 *
 * @author xiaochang
 * @date 2025-11-03
 */
@Service
@Slf4j
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService {
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private CheckInMapper checkInMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询护理任务
     *
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTask selectNursingTaskById(Long id) {
        return getById(id);
    }

    /**
     * 分页查询护理任务列表
     *
     * @param nursingTask 护理任务
     * @return 护理任务
     */
    @Override
    public List<NursingTask> selectNursingTaskList(NursingTask nursingTask) {

        return nursingTaskMapper.selectNursingTaskList(nursingTask);
    }

    /**
     * 新增护理任务
     *
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask) {
        return save(nursingTask) ? 1 : 0;
    }

    /**
     * 修改护理任务
     *
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask) {
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 批量删除护理任务
     *
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理任务信息
     *
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 根据id查询护理任务的详细信息
     *
     * @param id
     * @return
     */
    @Override
    public NursingTaskVo selectNursingById(Long id) {
        //创建一个集合，用于存储护理任务信息
        NursingTask nursingTask = lambdaQuery().eq(NursingTask::getId, id).one();
        if (nursingTask == null) {
            return null;
        }
        NursingTaskVo nursingTaskVo = BeanUtil.toBean(nursingTask, NursingTaskVo.class);
        //设置年龄
        CheckIn checkIn = checkInMapper.selectCheckInById(nursingTask.getElderId());
        nursingTaskVo.setAge(IDCardUtils.getAgeByIdCard(checkIn.getIdCardNo()));
        //设置护理人员的名字
        List<String> NickNameList = new ArrayList<>();
        if (nursingTask.getNursingId() != null){
            Long[] array = Arrays.stream(nursingTask.getNursingId().split(","))
                    .map(Long::parseLong)
                    .toArray(Long[]::new);
            for (Long nursingId : array) {
                String nickName = sysUserMapper.selectUserById(nursingId).getNickName();
                NickNameList.add(nickName);
            }
            //遍历护理人员id
            nursingTaskVo.setNursingName(NickNameList);
        }
        //查询护理等级的名称
        String nursingLenveName = checkInMapper.selectCheckInById(nursingTask.getElderId()).getNursingLevelName();
        nursingTaskVo.setNursingLevelName(nursingLenveName);
        //获取创建人的姓名
        SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(nursingTask.getCreateBy()));
        nursingTaskVo.setCreateBy(sysUser.getNickName());
        //nursingTaskVo.setCreateBy(sysUser.getUpdateBy());
        //获取执行人的名称
        String nickName = sysUserMapper.selectUserById(SecurityUtils.getUserId()).getNickName();
        nursingTaskVo.setUpdater(nickName);
       // nursingTaskVo.setCreateByName(sysUser.getNickName());

        //格式化创建时间
        String formatTime = nursingTask.getCreateTime().toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        nursingTaskVo.setCreateTime(formatTime);
        return nursingTaskVo;
    }


    /**
     * 取消护理任务
     *
     * @param
     * @return 删除结果
     */
    @Override
    public void cancelTask(NursingTaskDto nursingTaskDto) {
        //更新数据库
        lambdaUpdate()
                .set(NursingTask::getStatus, 3)
                .set(NursingTask::getCancelReason, nursingTaskDto.getReason())
                .eq(NursingTask::getId, nursingTaskDto.getTaskId())
                .update();

    }

    /**
     * 执行护理任务
     *
     * @param
     * @return 删除结果
     */
    @Override
    public void doTask(NursingTaskDto nursingTaskDto) {
        //更新数据库
        lambdaUpdate()
                // 添加id条件
                .eq(NursingTask::getId, nursingTaskDto.getTaskId())
                .set(NursingTask::getStatus, 2)
                .set(NursingTask::getRealServerTime, nursingTaskDto.getEstimatedServerTime())
                .set(NursingTask::getMark, nursingTaskDto.getMark())
                .set(NursingTask::getTaskImage, nursingTaskDto.getTaskImage())
                //设置执行人
                .update();

    }

    @Override
    public void adjustTask(NursingTaskDto nursingTaskDto) {
        lambdaUpdate()
                .eq(NursingTask::getId, nursingTaskDto.getTaskId())
                .set(NursingTask::getEstimatedServerTime, nursingTaskDto.getEstimatedServerTime())
                .update();

    }
}
